<script>
var comPort;
var station;
var title;
var videoID;

function setIconForYouTube(tabId, changeInfo, tab) {
    if (tab.url.indexOf("youtube.com/watch") > 0) {
        localStorage["icon"] = "jettube25.png";
        chrome.pageAction.setIcon({
            "tabId": tabId,
            "path": localStorage["icon"]
        });
        chrome.pageAction.show(tabId);
    }
}

chrome.tabs.onUpdated.addListener(setIconForYouTube);


function sendToJetTube(songurl, songtitle) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function (data) {
        if (xhr.readyState == 4) {
            comPort.postMessage({
                status: data.srcElement.responseText,
                station: station
            });
        }
    };

    var url = 'http://thejettube.appspot.com/addsong?songid=' + songurl + '&songtitle=' + songtitle + '&station=' + station.replace(' ','+');
    xhr.open('GET', url, true);
    xhr.send();
};

function comListener(msg) {
    title = msg.title;
    videoID = msg.videoID;
    }
    
        


function addPort(port) {
    comPort = port;
    comPort.onMessage.addListener(comListener);
}

function getStation(msg)
{
station = msg.station;
sendToJetTube(videoID, title,station);
}

chrome.extension.onRequest.addListener(getStation);
chrome.extension.onConnect.addListener(addPort);
</script>